/*
	
*/

package net.sf.odinms.server.maps;

import java.awt.Rectangle;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.net.MaplePacket;
import net.sf.odinms.scripting.reactor.ReactorScriptManager;
import net.sf.odinms.server.TimerManager;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.Pair;

/**
 *
 * @author Lerk
 */
public class MapleReactor extends AbstractMapleMapObject {

    private int rid;
    private MapleReactorStats stats;
    private byte state;
    private int delay;
    private MapleMap map;
    private String name;
    private boolean timerActive;
    private boolean alive;

    public MapleReactor(MapleReactorStats stats, int rid) {
        this.stats = stats;
        this.rid = rid;
        alive = true;
    }

    public void setTimerActive(boolean active) {
        this.timerActive = active;
    }

    public boolean isTimerActive() {
        return timerActive;
    }

    public void setState(byte state) {
        this.state = state;
    }

    public byte getState() {
        return state;
    }

    public int getId() {
        return rid;
    }

    public void setDelay(int delay) {
        this.delay = delay;
    }

    public int getDelay() {
        return delay;
    }

    @Override
    public MapleMapObjectType getType() {
        return MapleMapObjectType.REACTOR;
    }

    public int getReactorType() {
        return stats.getType(state);
    }

    public void setMap(MapleMap map) {
        this.map = map;
    }

    public MapleMap getMap() {
        return map;
    }

    public Pair<Integer, Integer> getReactItem() {
        return stats.getReactItem(state);
    }

    public boolean isAlive() {
        return alive;
    }

    public void setAlive(boolean alive) {
        this.alive = alive;
    }

    @Override
    public void sendDestroyData(MapleClient client) {
        client.getSession().write(makeDestroyData());
    }

    public MaplePacket makeDestroyData() {
        return MaplePacketCreator.destroyReactor(this);
    }

    @Override
    public void sendSpawnData(MapleClient client) {
        client.getSession().write(makeSpawnData());
    }

    public MaplePacket makeSpawnData() {
        return MaplePacketCreator.spawnReactor(this);
    }

    public void delayedHitReactor(final MapleClient c, long delay) {
        TimerManager.getInstance().schedule(new Runnable() {

            @Override
            public void run() {
                hitReactor(c);
            }
        }, delay);
    }
    
    //hitReactor command for item-triggered reactors
    public void hitReactor(MapleClient c) {
        hitReactor(0, (short) 0, c);
    }

    public void hitReactor(int charPos, short stance, MapleClient c) {
        //System.out.println("NextState："+stats.getNextState(state));
        if (stats.getType(state) < 999 && stats.getType(state) != -1) { //999是观赏性reactor 例如扎昆/闹钟的门 当前state是-1代表此reactor已被销毁
            //type 2 = 此reactor只能从右边敲击[例如沼泽地的草] charPos == 0/2是指人物从左边敲击 0是空中 2是地上
            if (!(stats.getType(state) == 2 && (charPos == 0 || charPos == 2))) {
                /*
                 * 起始状态是0 以最终状态为4为例 则状态4就是要销毁的
                 * 如果现在状态是3 则stats.getNextState(state);获取4
                 * 再用一次 stats.getNextState(state) 获取的为 -1 就证明没有下一个state了
                 * 则next[4]的就是要销毁的状态 而不是next2次[-1]才是销毁的状态
                 */
                state = stats.getNextState(state); //递增
                if (stats.getNextState(state) == -1) { //reactor现在形态+2不存在 即是下一步就是销毁[最后一个state就是销毁状态]
                    if (stats.getType(state) < 100) { //非物品触发类reactor
                        if (delay > 0) { //有重生时间的reactor
                            map.destroyReactor(getObjectId());
                        } else { //普通的reactor[此处敲击是把reactor弄成透明[最后一个state就是透明图片] 并没有真正销毁]
                            map.broadcastMessage(MaplePacketCreator.triggerReactor(this, stance));
                        }
                    } else { //物品触发的reactor触发后不消失 只是更新状态
                        map.broadcastMessage(MaplePacketCreator.triggerReactor(this, stance));
                    }
                    ReactorScriptManager.getInstance().act(c, this);
                } else { //继续触发reactor的下一形态 未销毁
                    map.broadcastMessage(MaplePacketCreator.triggerReactor(this, stance));
                    if (state == stats.getNextState(state)) { //状态一直想等的是循环类reactor
                        ReactorScriptManager.getInstance().act(c, this);
                    }
                }
            }
        } else {
            //这里是观赏性reactor 例如扎昆/闹钟的门
            state++; //进行状态递增
            map.broadcastMessage(MaplePacketCreator.triggerReactor(this, stance)); //敲击reactor
            ReactorScriptManager.getInstance().act(c, this);
        }
    }


    public Rectangle getArea() {
        return new Rectangle(getPosition().x + stats.getTL().x, getPosition().y + stats.getTL().y, stats.getBR().x - stats.getTL().x, stats.getBR().y - stats.getTL().y);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}